package offer;


public class erchashu55 {

    public static void main(String[] args) {
        erchashu55.Solution solution = new erchashu55().new Solution();
    }
//      Definition for a binary tree node.
    class Solution {
        public class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
            TreeNode(int x) { val = x; }
        }
        public boolean isBalanced(TreeNode root) {
            return height(root)>=0;
        }

        public int height(TreeNode root){
            if (root == null) return -1;
            int le = height(root.left);
            int ri = height(root.right);

            if (le==-1||ri==-1||Math.abs(le-ri)>1){
                return -1;
            }
            return Math.max(le,ri)+1;
        }
    }
}
